﻿using nplJobsCommon.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nplJobsDAL.Select
{
    public class SelectLocation : DataAccessBase
    {
        public int LocationID { get; set; }

        public SelectLocation()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectLocation.ToString();
        }

        #region GetCity
        public List<Location> GetLocation()
        {
            DataBaseHelper dbHelper;
            DataSet dsLocation;

            try
            {
                SelectLocationParameters selecCityParamenters = new SelectLocationParameters(this.LocationID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selecCityParamenters.Parameters;
                dsLocation = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsLocation);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region SQL parameters for selecting location  
        public class SelectLocationParameters
        {
            public int CkeckUnique;
            private SqlParameter[] _parameters;

            public SelectLocationParameters(int locationID)
            {
                this.LocationID = locationID;
                Build();
            }

            #region spAddContact parameters
            /*
         
         */
            #endregion

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@LocationID", LocationID),
                };

                Parameters = parameters;
            }

            public int LocationID { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion
        #region GetList
        private List<Location> GetList(DataSet dsLocation)
        {
            List<Location> locations;
            try
            {
                locations = (from n in dsLocation.Tables[0].AsEnumerable()
                             select new Location
                                {
                                    ID = n.Field<int>("ID"),
                                    Name = n.Field<string>("Name")
                                }).ToList();

                return locations;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}
